package com.scu.baseline.controller;

import com.scu.baseline.pojo.Task;
import com.scu.baseline.pojo.User;
import com.scu.baseline.service.impl.TaskServiceImpl;
import com.scu.baseline.service.impl.UserServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * (Task)表控制层
 *
 * @author lotswn
 * @since 2022-06-22 08:56:27
 * @version 1.0
 */
@RestController
@RequestMapping("/task")
@Api(value = "(Task)管理",tags = "(Task)管理接口API")
@ApiResponses({
        @ApiResponse(code=400,message="请求参数不完整或者错误"),
        @ApiResponse(code=404,message="找不到页面"),
        @ApiResponse(code=405,message="请求方式不正确，比如后台接收是post，但前端请求的是get"),
        @ApiResponse(code=406,message="页面地址和接口名冲突"),
        @ApiResponse(code=415,message="请求格式不正确，前端可能没有把请求头修改为json，也可能前端请求的json对象没有转换为字符串"),
        @ApiResponse(code=500,message="后台服务逻辑错误")
})
public class TaskController {
    /**
     * 服务对象
     */
    @Resource
    private TaskServiceImpl taskServiceImpl;
    /**
     * 通过主键查询单条数据
     * @param page  需要查询的页码
     * @param taskname  模糊查询的内容
     * @param hostid  模糊查询的内容
     * @param userid  模糊查询的内容
     * @return 单条数据
     */
    @RequestMapping(value = "/selectByPage", method = RequestMethod.GET)
    @ApiOperation(value = "列表查询分页接口",notes = "列表查询分页接口",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(name = "taskname",value = "任务名称",paramType = "query",dataType = "String",required = false),
            @ApiImplicitParam(name = "hostid",value = "主机编号",paramType = "query",dataType = "String",required = false),
            @ApiImplicitParam(name = "userid",value = "用户编号",paramType = "query",dataType = "String",required = false),
            @ApiImplicitParam(name = "status",value = "完成状态",paramType = "query",dataType = "String",required = false)
    })
    public Map<String, Object> selectByPage(@RequestParam(name = "page") int page, String taskname, String hostid, String userid, String status) {
        return this.taskServiceImpl.selectByPage(page, taskname, hostid, userid, status);
    }

    /**
     * 查询所有数据
     * @return 返回所有数据
     */
    @RequestMapping(value = "/selectAll", method = RequestMethod.GET)
    @ApiOperation(value = "查询所有数据接口",notes = "查询所有数据接口",httpMethod = "GET")
    public Map<String, Object> selectAll() {
        return this.taskServiceImpl.selectAll();
    }
    
    /**
     * 通过主键删除单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @RequestMapping(value = "/deleteById", method = RequestMethod.POST)
    @ApiOperation(value = "根据一个主键删除一条任务记录的接口",notes = "根据一个主键删除一条任务记录的接口",httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "主键",paramType = "query",dataType = "String",required = true),
    })
    public Map<String, Object> deleteById(@RequestParam("id") String id) {
        return this.taskServiceImpl.deleteById(id);
    }

    /**
     * 通过主机id查找待办任务
     *
     * @param hostid 主机id
     * @return 多条任务
     */
    @RequestMapping(value = "/pullTask", method = RequestMethod.POST)
    @ApiOperation(value = "通过主机id查找待办任务的接口",notes = "通过主机id查找待办任务的接口",httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "hostid",value = "主机编号",paramType = "query",dataType = "String",required = true),
    })
    public Map<String, Object> pullTask(@RequestParam("hostid") String hostid) {
        return this.taskServiceImpl.selectByHostId(Integer.valueOf(hostid));
    }


    /**
     * 通过主键删除多条数据
     *
     * @param ids 多个主键
     * @return map
     */
    @RequestMapping(value = "/batchDeleteByIds", method = RequestMethod.POST)
    @ApiOperation(value = "根据主键批量删除任务的接口",notes = "根据主键批量删除任务的接口",httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids",value = "多个主键",paramType = "query",dataType = "String",required = true),
    })
    public Map<String, Object> batchDeleteByIds(@RequestParam("ids") String ids) {
        String[] as = ids.split(",");
        System.out.println(as);
        for (int id = 0; id < as.length; id++) {
            this.taskServiceImpl.deleteById(as[id]);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "删除成功");
        return map;
    }


    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @RequestMapping(value = "/selectById", method = RequestMethod.GET)
    @ApiOperation(value = "通过主键查询一个任务的接口",notes = "通过主键查询一个任务的接口",httpMethod = "GET")
    public Map<String, Object> selectById(Integer id) {
        return this.taskServiceImpl.selectById(id);
    }
    
    /**
     * 通过主键更新单条数据
     *
     * @param task 一个数据库对应的POJO数据对象
     * @return 单条数据
     */
    @RequestMapping(value = "/updateById", method = RequestMethod.POST)
    @ApiOperation(value = "通过主键更新一个任务的接口",notes = "通过主键更新一个任务的接口",httpMethod = "POST")
    public Map<String, Object> updateById(@RequestBody @ApiParam(name = "task",value = "pojo模型",required = true) Task task) {
        return this.taskServiceImpl.updateById(task);
    }

    /**
     * 通过ID更新数据状态
     *
     * @param id 主键
     * @return 实例对象
     */
    @RequestMapping(value = "/pushTask", method = RequestMethod.POST)
    @ApiOperation(value = "通过ID更新数据状态",notes = "通过ID更新数据状态",httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "任务id",paramType = "query",dataType = "String",required = true),
    })
    public Map<String, Object> pushTask(@RequestParam("id") String id) {
        return this.taskServiceImpl.updateStatusById(Integer.valueOf(id));
    }


    /**
     * 通过一个pojo对象新增单条数据
     *
     * @param task 一个数据库对应的POJO数据对象
     * @return 返回插入的主键id
     */
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ApiOperation(value = "根据完整记录新增一个任务的接口",notes = "根据完整记录新增一个任务的接口",httpMethod = "POST")
    public Map<String, Object> insert(@RequestBody @ApiParam(name = "task",value = "pojo模型",required = true) Task task) {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        task.setUserid(user.getId());
        task.setStatus(0);
        return this.taskServiceImpl.insert(task);
    }
}